package com.robotic.sensor.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.robotic.common.annotation.Log;
import com.robotic.common.core.controller.BaseController;
import com.robotic.common.core.domain.AjaxResult;
import com.robotic.common.enums.BusinessType;
import com.robotic.common.utils.poi.ExcelUtil;
import com.robotic.common.core.page.TableDataInfo;
import com.robotic.sensor.domain.RegionStandard;
import com.robotic.sensor.service.IRegionStandardService;

/**
 * 区域标准配置Controller
 * 
 * @author robotic
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/sensor/regionStandard")
public class RegionStandardController extends BaseController
{
    @Autowired
    private IRegionStandardService regionStandardService;

    /**
     * 查询区域标准配置列表
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegionStandard regionStandard)
    {
        startPage();
        List<RegionStandard> list = regionStandardService.selectRegionStandardList(regionStandard);
        return getDataTable(list);
    }

    /**
     * 查询启用状态的区域标准配置列表
     */
    @GetMapping("/active")
    public AjaxResult getActiveList(String regionCode)
    {
        List<RegionStandard> list = regionStandardService.selectActiveRegionStandardList(regionCode);
        return AjaxResult.success(list);
    }

    /**
     * 根据区域代码查询所有传感器类型的标准配置
     */
    @GetMapping("/region/{regionCode}")
    public AjaxResult getByRegionCode(@PathVariable("regionCode") String regionCode)
    {
        List<RegionStandard> list = regionStandardService.selectRegionStandardByCode(regionCode);
        return AjaxResult.success(list);
    }

    /**
     * 根据传感器类型查询所有区域的标准配置
     */
    @GetMapping("/sensor/{sensorType}")
    public AjaxResult getBySensorType(@PathVariable("sensorType") String sensorType)
    {
        List<RegionStandard> list = regionStandardService.selectRegionStandardByType(sensorType);
        return AjaxResult.success(list);
    }

    /**
     * 导出区域标准配置列表
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:export')")
    @Log(title = "区域标准配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegionStandard regionStandard)
    {
        List<RegionStandard> list = regionStandardService.selectRegionStandardList(regionStandard);
        ExcelUtil<RegionStandard> util = new ExcelUtil<RegionStandard>(RegionStandard.class);
        util.exportExcel(response, list, "区域标准配置数据");
    }

    /**
     * 获取区域标准配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(regionStandardService.selectRegionStandardById(id));
    }

    /**
     * 根据区域代码和传感器类型获取区域标准配置
     */
    @GetMapping("/query")
    public AjaxResult getByCodeAndType(String regionCode, String sensorType)
    {
        RegionStandard regionStandard = regionStandardService.selectRegionStandardByCodeAndType(regionCode, sensorType);
        return AjaxResult.success(regionStandard);
    }

    /**
     * 新增区域标准配置
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:add')")
    @Log(title = "区域标准配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegionStandard regionStandard)
    {
        if (!"0".equals(regionStandardService.checkRegionStandardUnique(regionStandard)))
        {
            return AjaxResult.error("新增区域标准配置'" + regionStandard.getRegionCode() + "-" + regionStandard.getSensorType() + "'失败，区域代码和传感器类型组合已存在");
        }
        regionStandard.setCreateBy(getUsername());
        return toAjax(regionStandardService.insertRegionStandard(regionStandard));
    }

    /**
     * 修改区域标准配置
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:edit')")
    @Log(title = "区域标准配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegionStandard regionStandard)
    {
        if (!"0".equals(regionStandardService.checkRegionStandardUnique(regionStandard)))
        {
            return AjaxResult.error("修改区域标准配置'" + regionStandard.getRegionCode() + "-" + regionStandard.getSensorType() + "'失败，区域代码和传感器类型组合已存在");
        }
        regionStandard.setUpdateBy(getUsername());
        return toAjax(regionStandardService.updateRegionStandard(regionStandard));
    }

    /**
     * 更新区域标准配置状态
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:edit')")
    @Log(title = "区域标准配置状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody RegionStandard regionStandard)
    {
        return toAjax(regionStandardService.updateRegionStandardStatus(regionStandard.getId(), regionStandard.getStatus()));
    }

    /**
     * 删除区域标准配置
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:remove')")
    @Log(title = "区域标准配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionStandardService.deleteRegionStandardByIds(ids));
    }

    /**
     * 批量插入区域标准配置
     */
    @PreAuthorize("@ss.hasPermi('sensor:regionStandard:add')")
    @Log(title = "区域标准配置批量插入", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchInsert(@RequestBody List<RegionStandard> regionStandardList)
    {
        // 设置创建人
        for (RegionStandard regionStandard : regionStandardList)
        {
            regionStandard.setCreateBy(getUsername());
        }
        return toAjax(regionStandardService.batchInsertRegionStandard(regionStandardList));
    }

    /**
     * 校验区域代码和传感器类型组合是否唯一
     */
    @PostMapping("/checkUnique")
    public AjaxResult checkUnique(@RequestBody RegionStandard regionStandard)
    {
        String result = regionStandardService.checkRegionStandardUnique(regionStandard);
        if ("0".equals(result))
        {
            return AjaxResult.success("区域代码和传感器类型组合可用");
        }
        else
        {
            return AjaxResult.error(result);
        }
    }
}